package com.candoo.oil.scan;

import android.app.ProgressDialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.Toast;

import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;

public class OrderDetailActivity extends AppCompatActivity {


    private  String _oid="";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_detail);

        _init();


/*
        ListView list = (ListView) findViewById(R.id.listViewDetail);
        ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
        for(int i=0;i<30;i++)
        {
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("ItemTitle", "This is Title.....");
            map.put("ItemText", "This is text.....");
            mylist.add(map);
        }
        //生成适配器，数组===》ListItem
        SimpleAdapter mSchedule = new SimpleAdapter(this, //没什么解释
                mylist,//数据来源
                R.layout.listitem_detail,//ListItem的XML实现

                //动态数组与ListItem对应的子项
                new String[] {"ItemTitle", "ItemText"},

                //ListItem的XML文件里面的两个TextView ID
                new int[] {R.id.ItemTitle,R.id.ItemText});
        //添加并且显示
        list.setAdapter(mSchedule);
*/







        Intent intent = getIntent();
        _oid = intent.getStringExtra("oid");
        Log.e("oid",_oid);
        _read();


    }

    private ArrayList<P_Item> _listItem;
    private ArrayList<Map<String, Object>> listMap;
    private ListView listViewData;
private void _init()
{
    listViewData=(ListView) findViewById(R.id.listViewDetail);
    _listItem=new ArrayList<P_Item>();

    listViewData.setClickable(true);
    listViewData.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {

            P_Item item=_listItem.get(position);
            //Log.e("点击",""+position);
           // Log.e("点击",item.toString());
            openScan(item);
    /* write you handling code like...
    String st = "sdcard/";
    File f = new File(st+o.toString());
    // do whatever u want to do with 'f' File object
    */
        }
    });
}

    private void addToList(final List<P_Item> list, final P_Item item){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {

                //第一次读入数据
                if(list.isEmpty()){
                    list.add(item);
                }else{
                    for(int i = 0; i < list.size(); i++){
                        P_Item mEPC = list.get(i);

                        if(mEPC.Id==item.Id)
                        {
                            list.set(i,item);
                            break;
                        }else if(i == (list.size() - 1)){
                            list.add(item);
                        }
                    }
                }
                _flush();

            }
        });
    }

    private void _flush()
    {

        //将数据添加到ListView
        listMap = new ArrayList<Map<String,Object>>();
        for(P_Item epcdata:_listItem){
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("ItemTitle", epcdata.Name);
            map.put("ItemText", "SN:"+epcdata.Id+" 型号:"+epcdata.Model+" 扫描数:"+epcdata.Count_Used+"/"+epcdata.Count);


            listMap.add(map);
        }
        listViewData.setAdapter(new SimpleAdapter(OrderDetailActivity.this,
                listMap, R.layout.listitem_detail,
                new String[]{"ItemTitle", "ItemText"},
                new int[]{R.id.ItemTitle, R.id.ItemText}));

    }

   // private void

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 1000 && resultCode == 1001)
        {
            String _id=data.getStringExtra("item_id");
            String[] _list=data.getStringArrayExtra("item_list");
           // Log.e("Scan_S",_id);
           // Log.e("Scan_S",_list.toString());

            for(int i = 0; i < _listItem.size(); i++){
                P_Item mEPC = _listItem.get(i);
               // Log.e("Scan_S",mEPC.Id+"__"+_id);
                if(mEPC.Id.equals(_id))
                {
                   // Log.e("Scan_S","找到数据");
                    mEPC.List=_list;
                    mEPC.Count_Used=_list.length;
                    mEPC.Status=mEPC.Count_Used==mEPC.Count?1:0;
                    _listItem.set(i,mEPC);
                    break;
                }
            }
            _flush();
        }
    }

    public void openScan(P_Item item)
    {
        Intent intent = new Intent(this, ScanActivity.class);
        intent.putExtra("item_id", item.Id);
        intent.putExtra("item_name", item.Name);
        intent.putExtra("item_count", item.Count);
       // startActivity(intent);
        startActivityForResult(intent, 1000);
    }
    private P_Response_Read _rinfo;
    private P_Response_Save _sinfo;
    private ProgressDialog _pd;

    private void Read_Result()
    {
        //listEPC.removeAll(listEPC);
        //listViewData.setAdapter(null);
        _pd.cancel();
        if(_rinfo.flag==0)
        {
            //TextView _tv_oid=(TextView) findViewById(R.id.tv_oid);
            ((TextView) findViewById(R.id.tv_oid)).setText("单据号:"+_rinfo.oId);
            ((TextView) findViewById(R.id.tv_type)).setText("单据类型:"+_rinfo.Type);
            ((TextView) findViewById(R.id.tv_warehouse)).setText("仓库:"+_rinfo.Warehouse);
            ((TextView) findViewById(R.id.tv_supplier)).setText("供应商/客户:"+_rinfo.Supplier);
            for(P_Item item:_rinfo.list){
                addToList(_listItem,item);
            }
        }
        else
        {
            Toast.makeText(getApplicationContext(), _rinfo.msg,
                    Toast.LENGTH_SHORT).show();
            click_close_detail(null);
        }


    }
    private void Save_Result()
    {
        _pd.cancel();
     //  Log.e("ddd",_sinfo.toString());
        if(_sinfo.flag==0)
        {
            Toast.makeText(getApplicationContext(), _sinfo.msg,
                    Toast.LENGTH_SHORT).show();
            click_close_detail(null);
        }
        else
        {
            Toast.makeText(getApplicationContext(), _sinfo.msg,
                    Toast.LENGTH_SHORT).show();

        }
    }
    private void _read()
    {
        _pd= ProgressDialog.show(this,"","正在读取信息...");
        new Thread(){
            @Override
            public void run()
            {

                _rinfo=     httpClientClass.Read(_oid);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Read_Result();

                    }
                });
            }
        }.start();
    }

    public  void  click_close_detail(View view)
    {
        finish();
    }
    public void click_save(View view)
    {
        _pd= ProgressDialog.show(this,"","正在保存...");
        new Thread(){
            @Override
            public void run()
            {

                _sinfo=     httpClientClass.Save(_oid,_listItem);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Save_Result();
                    }
                });
            }
        }.start();
    }
}
